<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function jsonp({url,params,cb}){
            return new Promise((resolve,reject)=>{
                let script = document.createElement('script'); //创建script标签
                window[cb]=function(data){   //全局的函数 函数名为cb
                    resolve(data);
                    document.body.removeChild(script);//把script移除
                }
                params = {...params,cb} //  sort=  & query=
                let arrs = [];
                for(let key in params){
                    arrs.push(`${key}=${params[key]}`);
                }
                script.src = `${url}?${arrs.join('&')}`;
                console.log(script);
                document.body.appendChild(script);  //把script渲染到body上
            })
        }
        // 只能发送get请求 不支持post put delete
        // 不安全 xss攻击  不采用
        jsonp({
            url:'http://localhost:3000/say',
            params:{wd:'客户端'},
            cb:'show'   //这方法传入then里去
        }).then(data=>{
            console.log(data);
        })
    </script>
   
    
</body>
</html>